package r_Inst;

import mips.Decoder;
import mips.PC;
import mips.Register;


public class And {
	

	public static void and (int dest,int sc1,int sc2)
	{
		int source1 = toBinary(Register.registers[sc1]);
		int source2 = toBinary(Register.registers[sc2]);
		
		String source1S = source1 +"";
		String source2S = source2 +"";
		String deste = "";
		
		
		
		int diff = source1S.length() - source2S.length();
		if(diff>=0)
		{
		
		source2S = Decoder.AddzerostoLeft(source2S,source1S.length());
		}
		else
		{
		source1S = Decoder.AddzerostoLeft(source1S,source2S.length());

		}
		
		//System.out.println(source1S);
		//System.out.println(source2S);
		
		for(int i =0;i<source1S.length();i++)
		{
			if(source1S.charAt(i) =='1' && source2S.charAt(i)== '1'){
		
			deste=deste+"1";
		
			}
			else
				deste=deste+"0";
			
		}
		//System.out.println(source1S);
		//System.out.println(source2S);
		
		String resultB = deste;
		
		//System.out.println(resultB);
		
		
		Register.registers[dest]  = toInt(resultB + "");
		
		int source1Value = Register.registers[sc1];
		int source2Value = Register.registers[sc2];
		
System.out.println("PC value now is : " +Decoder.toBinary(PC.pc));  // add pc 
		
		System.out.println("PC value :"+ Decoder.toBinary(PC.pc)+" sent to The instruction memory to read address..");
		
		System.out.println("Instuction memory sending Instruction[25:21] : "+Decoder.AddzerostoLeft(Decoder.toBinary(sc1)+"",5)+" to Registers @ read Register1");
		System.out.println("Instuction memory sending Instruction[20:16]: "+Decoder.AddzerostoLeft(Decoder.toBinary(sc2)+"",5)+"  to Registers @ read Register2");
		
		System.out.println("Register number  read now is : " + Decoder.AddzerostoLeft(Decoder.toBinary(sc1)+"",5));
		System.out.println("Register2 number read now is : " + Decoder.AddzerostoLeft(Decoder.toBinary(sc2)+"",5));

		System.out.println("Instuction memory sending Instruction[15:11]: "+Decoder.AddzerostoLeft(Decoder.toBinary(dest)+"",5)+"  to mux and mux selection 1 occured and value forwarded to Registers @ write Register" );
		System.out.println("Destination register  is : " + Decoder.AddzerostoLeft(Decoder.toBinary(dest)+"",5));

		System.out.println("Instuction memory sending Instruction[31:26] to CONTROL,Instruction[31:26] having : "+ "000000");
		System.out.println("CONTROL send ALUOP to ALU CONTROL");
 
		System.out.println("Instuction memory sending Instruction[5:0] to ALU CONTROL");
		
		System.out.println("Registers sends first source value : "+ Decoder.toBinary(source1Value) + " Second Source value : " + Decoder.toBinary(source2Value) + "  to ALU");
		System.out.println("ALU is doing the 'AND',AluOp:010 operating now...");
		System.out.println("Result is ready :" + Decoder.toBinary(Register.registers[dest]));
		System.out.println("Sending to the mux...");
		System.out.println("Mux selection is 0,Alu result : "+ Decoder.toBinary(Register.registers[dest]) +" is sent now to the registers @ write data");
		System.out.println("Register : " +  Decoder.AddzerostoLeft(Decoder.toBinary(dest)+"",5) + ",have value now : " +Decoder.toBinary(Register.registers[dest]));
		
		System.out.println("PC value sent to Add");
		PC.pc +=4;
		System.out.println("pc value now is incremented by 4 and passed to the mux :"+Decoder.toBinary(PC.pc)+",selection 0 occured");
		System.out.println("Mux sent back to PC");
		System.out.println("New value of PC now is : " + Decoder.toBinary(PC.pc));//add pc
		//System.out.println("Executing next Instruction....");
		
	}
	
	 public static int toBinary(int num){
	      
	       String nume = Integer.toBinaryString(num);
	      return Integer.parseInt(nume);
	   }
	 public static int toInt(String stringg){
		    
		 
		    return (Integer.parseInt(stringg,2));

		}
	 /*
	 public static void main (String []args)
	 {
		 Register.registers[0] = 100;
		 Register.registers[1] = 20;
		 
		 And.and(2,0,1);
		 
		 System.out.println(Register.registers[2]+","+Register.registers[0]+","+Register.registers[1])
	 ;
	 
	 }*/


}

